var drugData = [{drugName:"未查询到相关药品,请输入正确的药品名或输入更多的信息"}];
var drugList=[];
var drugIndex = 0;
//延迟加载
$(function () {
    $("#example").dataTable({
        "iDeferLoading":500
    });
});

getDrug();
//获取全部药品信息
function getDrug(){
    $.ajax({
        url: "getDrugInfo",
        type: "post",
        dataType: "json",
        success:function (data) {
            if(data !=null){
                if(data.length > 0){
                    drugData = data;
                    console.log(drugData);
                }else{
                    drugData = [{drugName:"未查询到相关药品,请输入正确的药品名或输入更多的信息"}];
                }
            }else{
                drugData = [{drugName:"未查询到相关药品,请输入正确的药品名或输入更多的信息"}];
            }
        }
    });
}
//搜索候选列表
$(document).on("click","#searchText ul li",function(){
    $("#dgPrice").val($(this).attr("value"));
    $("#dgName").val($(this).text());
    $("#searchText").addClass("do_hide");
    $("#dgCount").attr('placeholder',"数量(当前库存:"+$(this).val()+")");
});
//药品数量输入框
$(document).on("input propertychange", "#dgCount", function () {
    if($(this).val() < 1){
        $(this).val(1);
    }
});

//药品搜索输入框
$(document).on("input propertychange", "#dgName", function () {
    var searchVal = $(this).val();
    if (!searchVal) {
        $("#searchText").addClass("do_hide");
    } else {
        if(drugData == undefined || drugData.length < 1 || drugData == null){
            getDrug();
        }
        $("#searchText").removeClass("do_hide");
        var len = drugData.length;
        var myUl = $("#searchText ul");
        myUl.children().remove();
        for(var i=0;i<len;i++){
            if(myUl.children().length < 1){
                myUl.append($("<li id='liTag'>未查询到相关药品,请输入正确的药品名或输入更多的信息</li>"));
            }
            //如果字符串中不包含目标字符会返回-1
            if(drugData[i].drugName.indexOf(searchVal)>=0){
                if($("#liTag").length > 0){
                    $("#liTag").remove();
                }
                myUl.append($("<li value='"+drugData[i].drugPrice+"'>"+drugData[i].drugName+"</li>"));
            }
        }
    }
});

//添加药品按钮
$(document).on("click","#addDrug",function(){
    drugIndex++;
    var drugVal = $("#dgName").val();
    var drugCount = $("#dgCount").val();
    var drugPrice = $("#dgPrice").val();
    console.log("drugVal:"+drugVal);
    console.log("drugCount:"+drugCount);
    console.log("drugPrice:"+drugPrice);
    if(drugVal == "" || drugVal == null || drugCount == 0){
        //数据未完善
        $("#addMsg").text("请完善数据");
        $("#addMsg").removeClass("do_hide");
        setTimeout(function(){
            $("#addMsg").addClass("do_hide");
        },1500);
        return;
    }
    var flg = false;
    for(var i=0;i< drugData.length;i++){
        if(drugData[i].drugName == drugVal){
            flg = true;
            break;
        }
    }
    if(!flg){
        //数据不正确
        $("#addMsg").text("请输入正确的药品名");
        $("#addMsg").removeClass("do_hide");
        setTimeout(function(){
            $("#addMsg").addClass("do_hide");
        },1500);
        return;
    }
    var mainDiv = $("<div class='form-group row'></div>");
    var drugLbl = $("<label  class='col-form-label col-md-2' value='"+drugIndex+"'><a href='javascript:void(0)' onclick='delDrugItem("+drugIndex+",this)'>药品</a></label>");
    var nameDiv = $("<div class='col-md-4'></div>");
    var drugNameIpt = $("<input type='text' readonly='readonly' name='drugName' value='"+drugVal+"' class='form-control'>");
    var drugCountLbl = $("<label id='priceLbl' class='col-form-label col-md-1' value='"+drugPrice+"'>数量</label>");
    var numDiv = $("<div class='col-md-5'></div>");
    var drugCountIpt = $("<input type=\"text\" readonly='readonly' name='drugCount' value='"+drugCount+"' class='form-control'>");
    drugList.push({
        drugIndex:drugIndex,
        medicalId:0,
        drugVal:drugVal,
        drugCount:drugCount,
        drugPrice:drugPrice
    });
    console.log(drugList);
    nameDiv.append(drugNameIpt);
    numDiv.append(drugCountIpt);

    mainDiv.append(drugLbl).append(nameDiv).append(drugCountLbl).append(numDiv);
    $("#drugList").append(mainDiv);
    $("#closeBtn").click();
    $("#dgName").val("");
    $("#dgCount").val(1);
});

function delDrugItem(drugIndex,tag){
    $(tag).parent().parent().remove();
    for(var i=0;i< drugList.length;i++){
        if(drugList[i].drugIndex == drugIndex){
            drugList.splice(i,1);
        }
    }
}


$(document).on("click","#smt",function(){
    var medicalId = $("#medicalId").val();
    console.log(medicalId);
    if(!isInteger(medicalId) || medicalId== undefined || medicalId =="" || medicalId == null || medicalId <= 0){
        myMSG("请输入正确的诊断信息ID",false);
        return;
    }
    if(drugList.length < 1){
        myMSG("请添加药品",false);
        return;
    }
    drugList.push({
        medicalId:medicalId,
        drugVal:0,
        drugCount:""
    });
    $.ajax({
        url:"addPayInfo",
        type:"post",
        contentType : 'application/json;charset=utf-8',
        async:true,
        data:JSON.stringify(drugList),
        success:function(data){
            if(data != null){
                if(data == -999){
                    myMSG("请输入正确的诊断信息ID",false);
                }else if(data == -9999){
                    myMSG("诊断信息ID有误",false);
                }else if(data > 0){
                    $("#medicalId").val("");
                    $("#drugList").children().remove();
                    myMSG("提交成功",true);
                }
            }
        }
    });
});

function isInteger(obj){
    return obj%1 ===0;
}

function myMSG(show,tag){
    $("#msg").text(show);
    if(tag){
        $("#msg").css({
            "color":"green"
        });
    }else{
        $("#msg").css({
            "color":"red"
        });
    }
    $("#msg").removeClass("do_hide");
    setTimeout(function(){
        $("#msg").addClass("do_hide");
    },2000);
}

